﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Qianli.DirSir.Common;
using Qianli.DirSir.Model;
using System.Data;
using System.Data.SqlClient;

namespace Qianli.DirSir.DAL
{
    public class ProviderInfoDAL
    {
        SQLHelper helper = new SQLHelper();
        /// <summary>
        /// 显示供应商
        /// </summary>
        /// <returns></returns>
        public List<ProviderInfo> ProviderInfoShow()
        {
            DataTable dt= helper.ExecDataTable_Proc("ProviderInfo_show");
            List<ProviderInfo> list = new List<ProviderInfo>();
            if(dt.Rows.Count>0)
            {
                foreach (DataRow item in dt.Rows)
                {
                    ProviderInfo pro = new ProviderInfo();
                    pro.ProviderID = Convert.ToInt32(item["ProviderID"]);
                    pro.ProviderCoding = item["ProviderCoding"].ToString();
                    pro.ProviderName = item["ProviderName"].ToString();
                    pro.Contacts = item["Contacts"].ToString();
                    pro.Phone = item["Phone"].ToString();
                    pro.CityName = item["CityName"].ToString();
                    list.Add(pro);
                }
            }
            return list;
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="Shinfo"></param>
        /// <returns></returns>
        public int ProviderInfoAdd(ProviderInfo Provd)
        {
            if (Provd.ProviderCoding != "")
            {
               
                Provd.ProviderCoding = System.Guid.NewGuid().ToString();
                SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@ProviderCoding",SqlDbType.NVarChar,20),
                    new SqlParameter("@ProviderName",SqlDbType.NVarChar,20),
                    new SqlParameter("@Contacts",SqlDbType.NVarChar,20),
                    new SqlParameter("@Phone",SqlDbType.NVarChar,20),
                    new SqlParameter("@CityID",SqlDbType.Int,4)
                };
                para[0].Value = Provd.ProviderCoding;
                para[1].Value = Provd.ProviderName;
                para[2].Value = Provd.Contacts;
                para[3].Value = Provd.Phone;
                para[4].Value = Provd.CityID;
                return helper.ExecNonquery_Proc("ProviderInfo_Add",para);
            }
            else
            {
                SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@ProviderCoding",SqlDbType.NVarChar,20),
                    new SqlParameter("@ProviderName",SqlDbType.NVarChar,20),
                    new SqlParameter("@Contacts",SqlDbType.NVarChar,20),
                    new SqlParameter("@Phone",SqlDbType.NVarChar,20),
                    new SqlParameter("@CityID",SqlDbType.Int,4)
                };
                para[0].Value = Provd.ProviderCoding;
                para[1].Value = Provd.ProviderName;
                para[2].Value = Provd.Contacts;
                para[3].Value = Provd.Phone;
                para[4].Value = Provd.CityID;
                return helper.ExecNonquery_Proc("ProviderInfo_Add");
            }
        }
        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="Proid"></param>
        /// <returns></returns>
        public ProviderInfo ProviderInfoGetID(int Proid)
        {
            ProviderInfo pro=new ProviderInfo();
            SqlParameter[] par = new SqlParameter[]{
                new SqlParameter("@ProviderID",SqlDbType.Int)
            };
            par[0].Value = Proid;
            DataTable table = helper.ExecDataTable_Proc("ProviderInfo_GetID",par);
            pro.ProviderID = Convert.ToInt32(table.Rows[0]["ProviderID"]);
            pro.ProviderName = table.Rows[0]["ProviderName"].ToString();
            pro.Contacts = table.Rows[0]["Contacts"].ToString();
            pro.Phone = table.Rows[0]["Phone"].ToString();
            pro.CityID = Convert.ToInt32(table.Rows[0]["CityID"]);
            return pro;
        }
        /// <summary>
        /// 修改供应商信息
        /// </summary>
        /// <param name="Provd"></param>
        /// <returns></returns>
        public int ProviderInfoUpdate(ProviderInfo Provd)
        {
            SqlParameter[] para = new SqlParameter[]{
                    new SqlParameter("@ProviderID",SqlDbType.Int),
                    new SqlParameter("@ProviderName",SqlDbType.NVarChar,20),
                    new SqlParameter("@Contacts",SqlDbType.NVarChar,20),
                    new SqlParameter("@Phone",SqlDbType.NVarChar,20),
                    new SqlParameter("@CityID",SqlDbType.Int,4)
                };
            para[0].Value = Provd.ProviderID;
            para[1].Value = Provd.ProviderName;
            para[2].Value = Provd.Contacts;
            para[3].Value = Provd.Phone;
            para[4].Value = Provd.CityID;
            return helper.ExecNonquery_Proc("ProviderInfo_Update", para);
        }
        /// <summary>
        /// 根据ID删除
        /// </summary>
        /// <param name="Proid"></param>
        /// <returns></returns>
        public int ProviderInfoDelete(int Proid)
        {
            SqlParameter[] par = new SqlParameter[]{
                new SqlParameter("@ProviderID",SqlDbType.Int)
            };
            par[0].Value = Proid;
            return helper.ExecNonquery_Proc("ProviderInfo_Delete", par);
        }
        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="ProName"></param>
        /// <returns></returns>
        public List<ProviderInfo> ProviderInfoSelect(string ProName)
        {
            SqlParameter[] par = new SqlParameter[]{
                new SqlParameter("@ProviderName",SqlDbType.NVarChar,20)
            };
            par[0].Value = ProName;
            DataTable table = helper.ExecDataTable_Proc("ProviderInfo_Select", par);
            List<ProviderInfo> list = new List<ProviderInfo>();
            foreach (DataRow item in table.Rows)
            {
                ProviderInfo pro = new ProviderInfo();
                pro.ProviderID = Convert.ToInt32(item["ProviderID"]);
                pro.ProviderCoding = item["ProviderCoding"].ToString();
                pro.ProviderName = item["ProviderName"].ToString();
                pro.Contacts = item["Contacts"].ToString();
                pro.Phone = item["Phone"].ToString();
                pro.CityID = Convert.ToInt32(item["CityID"]);
                list.Add(pro);
            }
            return list;
        }
        
    }
}
